load("//bazel/config:copt.bzl", "ASYNC_SIMPLE_COPTS")

uthread_prefix = "async_simple/uthread/internal/"

uthread_srcs = [
] + select({
    "//bazel/config:async_simple_with_uthread": [
        "async_simple/uthread/internal/thread_impl.h",
        "async_simple/uthread/internal/thread.cc",
    ],
    "//conditions:default": [],
}) + select({
    "//bazel/config:linux_x86_64": glob([uthread_prefix + "Linux/" + "x86_64/*.S"]),
    "//bazel/config:linux_aarch64": glob([uthread_prefix + "Linux/" + "aarch64/*.S"]),
    "//bazel/config:linux_ppc64le": glob([uthread_prefix + "Linux/" + "ppc64le/*.S"]),
    "//bazel/config:darwin_x86_64": glob([uthread_prefix + "Darwin/" + "x86_64/*.S"]),
    "//bazel/config:darwin_aarch64": glob([uthread_prefix + "Darwin/" + "arm64/*.S"]),
    "//conditions:default": [],
})

uthread_hdrs = select({
    "//bazel/config:async_simple_with_uthread": glob([
        "async_simple/uthread/internal/*.h",
        "async_simple/uthread/*.h",
    ]),
    "//conditions:default": [],
})

cc_library(
    name = "simple_executors",
    hdrs = glob(["async_simple/executors/*.h"]),
    defines = select({
        "//bazel/config:async_simple_has_not_aio": ["ASYNC_SIMPLE_HAS_NOT_AIO"],
        "//conditions:default": [],
    }),
    linkopts = select({
        "//bazel/config:async_simple_has_not_aio": [],
        "//conditions:default": ["-laio"],
    }),
    visibility = ["//visibility:public"],
)

cc_library(
    name = "async_simple",
    srcs = [
        "async_simple/experimental/coroutine.h",
    ] + uthread_srcs,
    hdrs = glob([
        "async_simple/*.h",
        "async_simple/coro/*.h",
        "async_simple/util/*.h",
    ]) + uthread_hdrs,
    copts = ASYNC_SIMPLE_COPTS,
    linkopts = select({
        "@platforms//os:windows": [],
        "//conditions:default": ["-lpthread"],
    }),
    visibility = ["//visibility:public"],
)
